Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: type-safe JSON insertions/updates, eb.valJson & sql.valJson. #1130

Draft
wants to merge 13 commits into
base: v0.28
Choose a base branch
from

Conversation

igalklebanov
Copy link
Member

@igalklebanov igalklebanov commented Aug 25, 2024

Hey 👋

This PR deprecates JSONColumnType, and introduces the stricter Json type that enforces type-safe insertions/updates.

Using a branded type as the InsertType and UpdateType AND as the return value of new serialization helpers in ExpressionBuilder and sql template tag, users can now insert/update and serialize their JSON objects/arrays safely into JSON columns.

Compilers/plugins can serialize the objects/arrays differently, as the value is passed around in a ValueNode with a serialized flag. The default serialization method is, well, JSON.stringify.

db.insertInto('person_metadata')
  .values((eb) => ({
    website: eb.valJson({ url: 'https://kysely.dev' }),
    // or...
    experience: sql.valJson([{
      establishment: 'The University of Life',
    }]),
  }))

If you want the non-type-safe old JSONColumnType.. we don't encourage it, but you can continue using JSONColumnType (until we decide to remove it) or just use:

import type { ColumnType } from 'kysely'

type MetadataColumn = ColumnType<YourJSONType, string, string>

For a readonly Json, use:

import type { ColumnType, Json, Serialized } from 'kysely'

type MetadataColumn = Json<YourJSONType, Serialized<YourJSONType>, never>

@igalklebanov igalklebanov added api Related to library's API breaking change Includes breaking changes enhancement New feature or request labels Aug 25, 2024
Copy link

vercel bot commented Aug 25, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
kysely ✅ Ready (Inspect) Visit Preview 💬 Add feedback Nov 24, 2024 0:53am

@igalklebanov igalklebanov changed the title feat: Serialized<O>, stricter JSONColumnType<O>, eb.valSerialized & sql.valSerialized. feat: Serialized<O>, stricter JSONColumnType<O> insertions/updates, eb.valSerialized & sql.valSerialized. Aug 25, 2024
@igalklebanov igalklebanov changed the title feat: Serialized<O>, stricter JSONColumnType<O> insertions/updates, eb.valSerialized & sql.valSerialized. feat: stricter JSONColumnType<O> insertions/updates, eb.valSerialized & sql.valSerialized. Aug 25, 2024
@igalklebanov igalklebanov changed the title feat: stricter JSONColumnType<O> insertions/updates, eb.valSerialized & sql.valSerialized. feat: type-safe JSONColumnType<O> insertions/updates, eb.valSerialized & sql.valSerialized. Aug 25, 2024
@igalklebanov igalklebanov changed the title feat: type-safe JSONColumnType<O> insertions/updates, eb.valSerialized & sql.valSerialized. feat: type-safe JSONColumnType<O> insertions/updates, eb.valJson & sql.valJson. Sep 1, 2024
@igalklebanov igalklebanov removed the breaking change Includes breaking changes label Sep 2, 2024
@igalklebanov igalklebanov changed the title feat: type-safe JSONColumnType<O> insertions/updates, eb.valJson & sql.valJson. feat: type-safe JSON insertions/updates, eb.valJson & sql.valJson. Sep 3, 2024
@igalklebanov igalklebanov force-pushed the v0.28 branch 2 times, most recently from a84367a to 124c0dc Compare October 3, 2024 22:55
@igalklebanov igalklebanov force-pushed the v0.28 branch 3 times, most recently from b2b78db to ae5cb1e Compare October 24, 2024 12:39
igalklebanov and others added 11 commits November 24, 2024 02:32
…kysely-org#1085)

* add reusable helpers recipe and implement missing expression features

* force node 22.4.1 in CI because of an npm bug
* feat: add postgres range types (kysely-org#1086)

* feat: support refresh naterialized view

* fix tests by adding .materialized() to remove the matview

* fix failing test

* fix: References typo (kysely-org#1092)

* chore: refresh-view-node.ts => refresh-materialized-view-node.ts

* chore: export node in index.ts

---------

Co-authored-by: Isak <[email protected]>
Co-authored-by: Jonathan Wu <[email protected]>
introduce ValueNode.serialized.

introduce eb.valSerialized.

introduce sql.valSerialized.

fix json-traversal test suite.

fix null handling @ compiler.

rename to `valJson`.

add instructions in errors.

typings test inserts.

call the new type `Json` instead, to not introduce a breaking change.

add missing json column @ Getting Started.

add `appendSerializedValue`.
Copy link

pkg-pr-new bot commented Nov 24, 2024

Open in Stackblitzkysely_koa_example

npm i https://pkg.pr.new/kysely-org/kysely@1130

commit: fff673a

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api Related to library's API enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants